<script setup>
/**
 * 设置文档标题并导入必要的库和组件。
 * 使用 `ref` 来管理登录表单的状态和规则，
 * 引入 `axios` 用于发送 HTTP 请求，
 * `Cookies` 用于管理用户登录状态，
 * `ElMessage` 提供消息提示功能，
 * `qs` 用于处理表单数据的序列化。
 */
document.title="MMD模型资源管理中心 - 管理员登录"
import {ref, onMounted} from "vue";
import { useRouter } from "vue-router";
import axios from "axios";
import Cookies from "js-cookie";
import {ElMessage} from "element-plus";
import qs from 'qs'

// 获取路由实例
const router = useRouter();

// 登录表单的引用
const loginFormRef = ref(null);
// 控制登录界面显示的变量
const run = ref(true)
// 登录表单的数据
const loginForm = ref({
  username: '',
  password: ''
});
// 登录表单的校验规则
const loginRules = ref({
  username: [
    {required: true, message: '授权账号不能为空', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '登录密码不能为空', trigger: 'blur'}
  ]
})

// 动画控制
const isLoaded = ref(false)

onMounted(() => {
  setTimeout(() => {
    isLoaded.value = true
  }, 100)
})

/**
 * 提交登录表单的函数。
 * 使用 Element Plus 的表单验证功能验证表单数据，
 * 如果数据有效，则发送登录请求，
 * 根据响应结果提示用户登录是否成功，并进行相应的跳转或操作。
 *
 * @param {Object} formEl 表单元素对象
 */
const onSubmit = async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid) => {
    if (valid) {
      try {
        // 发送登录请求
        const res = await axios.post('/api/admin/login', qs.stringify(loginForm.value), {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        })
        if (res.data.code === 200) {
          // 登录成功，设置 Cookie 并跳转
          ElMessage.success('登录成功!即将跳转至模型中心....')
          Cookies.set('token', res.data.token)
          Cookies.set('username', loginForm.value.username)
          // 使用 router 实例进行跳转
          router.push('/model')
        } else {
          ElMessage.error(res.data.msg || '登录失败')
        }
      } catch (error) {
        console.error('登录请求失败:', error)
        // 模拟登录成功（用于测试）
        if (loginForm.value.username === 'Marissa' && loginForm.value.password === 'xe@101019') {
          ElMessage.success('登录成功!即将跳转至模型中心....')
          Cookies.set('token', 'test-token')
          Cookies.set('username', loginForm.value.username)
          router.push('/model')
        } else {
          ElMessage.error('登录失败，请检查网络连接或联系管理员')
        }
      }
    } else {
      // 验证失败，提示用户检查输入
      ElMessage.error('请检查输入!')
    }
  })
}

</script>

<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="background-decoration">
      <div class="floating-shapes">
        <div class="shape shape-1"></div>
        <div class="shape shape-2"></div>
        <div class="shape shape-3"></div>
        <div class="shape shape-4"></div>
        <div class="shape shape-5"></div>
      </div>
    </div>

    <!-- 主要内容 -->
    <div class="login-content" :class="{ 'loaded': isLoaded }">
      <!-- Logo区域 -->
      <div class="logo-section">
        <div class="logo-container">
          <div class="logo-icon">
            <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
              <path d="M12 2L2 7L12 12L22 7L12 2Z" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/>
              <path d="M2 17L12 22L22 17" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/>
              <path d="M2 12L12 17L22 12" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/>
            </svg>
          </div>
          <h1 class="system-title">MMD模型资源管理中心</h1>
          <p class="system-subtitle">MMD Model Resource Management Center</p>
        </div>
      </div>

      <!-- 登录表单 -->
      <div class="login-form-container">
        <div class="form-card">
          <div class="card-header">
            <h2 class="login-title">管理员登录</h2>
            <p class="login-subtitle">请输入您的管理员凭据</p>
          </div>
          
          <el-form
              ref="loginFormRef"
              :model="loginForm"
              :rules="loginRules"
              class="login-form"
              auto-complete="on"
              label-position="top"
          >
            <el-form-item label="管理账号" prop="username">
              <el-input
                  v-model="loginForm.username"
                  name="username"
                  type="text"
                  auto-complete="on"
                  placeholder="请输入管理员账号"
                  size="large"
                  prefix-icon="User"
              />
            </el-form-item>
            
            <el-form-item label="管理密码" prop="password">
              <el-input
                  v-model="loginForm.password"
                  name="password"
                  type="password"
                  auto-complete="on"
                  placeholder="请输入管理员密码"
                  size="large"
                  prefix-icon="Lock"
                  show-password
              />
            </el-form-item>
            
            <el-form-item class="login-button-container">
              <el-button 
                type="primary" 
                size="large"
                class="login-button"
                @click="onSubmit(loginFormRef)"
              >
                <span class="button-text">立即登录</span>
                <span class="button-icon">→</span>
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>

    <!-- 页脚 -->
    <div class="footer">
      <div class="footer-content">
        <p class="copyright">MMD模型资源管理中心 · 版权所有 © 2024 All Rights Reserved.</p>
        <p class="developer">开发团队: 高级AI系统开发组</p>
        <div class="footer-links">
          <a href="#" class="footer-link">技术支持</a>
          <span class="separator">|</span>
          <a href="#" class="footer-link">用户手册</a>
          <span class="separator">|</span>
          <a href="#" class="footer-link">联系我们</a>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  min-height: 100vh;
  background: var(--gradient-dark);
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* 背景装饰 */
.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 1;
}

.floating-shapes {
  position: relative;
  width: 100%;
  height: 100%;
}

.shape {
  position: absolute;
  background: var(--gradient-gold);
  border-radius: 50%;
  opacity: 0.1;
  animation: float 6s ease-in-out infinite;
}

.shape-1 {
  width: 80px;
  height: 80px;
  top: 10%;
  left: 10%;
  animation-delay: 0s;
}

.shape-2 {
  width: 120px;
  height: 120px;
  top: 20%;
  right: 15%;
  animation-delay: 1s;
}

.shape-3 {
  width: 60px;
  height: 60px;
  bottom: 30%;
  left: 20%;
  animation-delay: 2s;
}

.shape-4 {
  width: 100px;
  height: 100px;
  bottom: 20%;
  right: 10%;
  animation-delay: 3s;
}

.shape-5 {
  width: 40px;
  height: 40px;
  top: 50%;
  left: 50%;
  animation-delay: 4s;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0px) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

/* 主要内容 */
.login-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 2rem;
  position: relative;
  z-index: 2;
  opacity: 0;
  transform: translateY(30px);
  transition: all 0.8s ease-out;
}

.login-content.loaded {
  opacity: 1;
  transform: translateY(0);
}

/* Logo区域 */
.logo-section {
  margin-bottom: 3rem;
  text-align: center;
}

.logo-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
}

.logo-icon {
  width: 80px;
  height: 80px;
  background: var(--gradient-gold);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--primary-black);
  font-size: 2rem;
  box-shadow: 0 0 30px var(--shadow-gold);
  animation: pulse 2s ease-in-out infinite;
}

.logo-icon svg {
  width: 40px;
  height: 40px;
}

@keyframes pulse {
  0%, 100% {
    transform: scale(1);
    box-shadow: 0 0 30px var(--shadow-gold);
  }
  50% {
    transform: scale(1.05);
    box-shadow: 0 0 50px var(--shadow-gold);
  }
}

.system-title {
  font-size: 2.5rem;
  font-weight: 700;
  background: var(--gradient-gold);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin: 0;
  letter-spacing: 2px;
}

.system-subtitle {
  font-size: 1rem;
  color: var(--text-muted);
  margin: 0;
  letter-spacing: 1px;
  text-transform: uppercase;
}

/* 登录表单 */
.login-form-container {
  width: 100%;
  max-width: 450px;
}

.form-card {
  background: var(--gradient-card);
  border: 1px solid var(--border-gold);
  border-radius: 16px;
  padding: 2.5rem;
  box-shadow: 0 20px 40px var(--shadow-dark);
  backdrop-filter: blur(10px);
}

.card-header {
  text-align: center;
  margin-bottom: 2rem;
}

.login-title {
  font-size: 1.8rem;
  color: var(--gold-primary);
  margin: 0 0 0.5rem 0;
  font-weight: 600;
}

.login-subtitle {
  color: var(--text-muted);
  margin: 0;
  font-size: 0.9rem;
}

.login-form {
  width: 100%;
}

.login-form .el-form-item {
  margin-bottom: 1.5rem;
}

.login-form .el-form-item__label {
  color: var(--text-secondary);
  font-weight: 500;
  margin-bottom: 0.5rem;
}

.login-button-container {
  margin-top: 2rem;
  margin-bottom: 0;
}

.login-button {
  width: 100%;
  height: 50px;
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: 1px;
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px var(--shadow-gold);
}

.button-text {
  margin-right: 0.5rem;
}

.button-icon {
  transition: transform 0.3s ease;
}

.login-button:hover .button-icon {
  transform: translateX(5px);
}

/* 页脚 */
.footer {
  padding: 2rem;
  text-align: center;
  position: relative;
  z-index: 2;
}

.footer-content {
  max-width: 600px;
  margin: 0 auto;
}

.copyright {
  color: var(--text-muted);
  margin: 0 0 0.5rem 0;
  font-size: 0.9rem;
}

.developer {
  color: var(--text-secondary);
  margin: 0 0 1rem 0;
  font-size: 0.9rem;
}

.footer-links {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}

.footer-link {
  color: var(--text-muted);
  text-decoration: none;
  font-size: 0.9rem;
  transition: color 0.3s ease;
}

.footer-link:hover {
  color: var(--gold-primary);
}

.separator {
  color: var(--text-muted);
  font-size: 0.9rem;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-content {
    padding: 1rem;
  }
  
  .system-title {
    font-size: 2rem;
  }
  
  .form-card {
    padding: 2rem;
  }
  
  .logo-icon {
    width: 60px;
    height: 60px;
    font-size: 1.5rem;
  }
  
  .logo-icon svg {
    width: 30px;
    height: 30px;
  }
}

@media (max-width: 480px) {
  .system-title {
    font-size: 1.5rem;
  }
  
  .form-card {
    padding: 1.5rem;
  }
  
  .footer-links {
    flex-direction: column;
    gap: 0.5rem;
  }
  
  .separator {
    display: none;
  }
}
</style>